package com.barbeque.mall.controller;

import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.BaseRespVo;
import com.barbeque.mall.bean.advertise.*;
import com.barbeque.mall.bean.advertise.BO.AdListBo;
import com.barbeque.mall.bean.advertise.VO.MyBaseRespVo;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.service.AdService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * @author bai
 * @since 2022/07/15 22:20
 */
@RestController
@RequestMapping("/admin/ad")
public class AdController {
    @Autowired
    AdService adService;


    /**
     * 显示广告的列表及模糊查询
     * @param adListBo
     * @return
     */
    // localhost:8083/admin/ad/list?page=1&limit=20&name=t&content=&sort=add_time&order=desc
    //@RequiresPermissions("admin:ad:list")
    @RequiresPermissions("admin:ad:list")
    @RequestMapping(value = "list")
    public BaseRespVo list(AdListBo adListBo){
        CommonData<MarketAd> adList = new CommonData<>();

        if(adListBo.getName() == null && adListBo.getContent() == null){

            adList = adService.queryAdList(adListBo);

        }else {
            // 做模糊查询操作
            adList = adService.queryAdListByCondition(adListBo);
        }


        return BaseRespVo.ok(adList);
    }

    // localhost:8083/admin/ad/delete
    //@RequiresPermissions("admin:ad:delete")
    @RequestMapping("/delete")
    @RequiresPermissions("admin:ad:delete")
    public MyBaseRespVo delete(@RequestBody MarketAd marketAd){
        // 进行删除操作
        adService.deleteAd(marketAd);

        return MyBaseRespVo.ok();
    }

    // localhost:8083/admin/ad/create
    //@RequiresPermissions("admin:ad:create")
    @RequestMapping("create")
    @RequiresPermissions("admin:ad:create")
    public BaseRespVo create(@RequestBody MarketAd marketAd){

        marketAd = adService.insertAdInfo(marketAd);


        return BaseRespVo.ok(marketAd);
    }

    // localhost:8083/admin/ad/update
    //@RequiresPermissions("admin:ad:update")
    @RequestMapping("update")
    @RequiresPermissions("admin:ad:update")
    public BaseRespVo update(@RequestBody MarketAd marketAd){
        // 修改广告信息
        marketAd = adService.updateAdInfoById(marketAd);

        return BaseRespVo.ok(marketAd);
    }
}
